Apache Mesos Cookbook by David Blomquist

Apache Mesos Cookbook by David Blomquist

Author:David Blomquist
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2017-08-02T10:30:12+00:00


Registering frameworks

In this recipe, we will learn how frameworks register in Mesos to receive offers and state updates.

How to do it...

We will create the scheduler.go file and implement our framework inside of it.

Before we start, we need to define some globals and imports that we will need later:

import (

"bufio"

"bytes"

"log"

"net/http"

"os"

"strconv"

"strings"

"github.com/golang/protobuf/jsonpb"

)

// Url to Mesos master scheduler API

const schedulerApiUrl = "http://10.10.10.10:5050/api/v1/scheduler"

// Current framework configuration

var frameworkInfo FrameworkInfo

// Marshaler to serialize Protobuf Message to JSON

var marshaller = jsonpb.Marshaler{

EnumsAsInts: false,

Indent: " ",

OrigName: true,

}

jsonpb.Marshaler is a part of the Golang Protobuf binding. It's responsible for converting structs into JSON. We will use it to serialize the messages we send to Mesos. It's important to use a proper Marshaler because, as you can see, generated structs have additional struct tags with serialization hints. For example, enums are stored as integers but Mesos requires them as text; the default JSON Marshaler will ignore these hints and pass them as Ints.

The next step is to define the main() function. In main, we will populate frameworkInfo with the values required to register the framework. In this simple example, we don't use security features or roles, but if we do, frameworkInfo is the place where this could be set:

func main() {

user := "root"

name := "simple_framework"

hostname, err := os.Hostname()

if err != nil {

log.Fatal(err)

}

frameworkInfo = FrameworkInfo{

User: &user,

Name: &name,

Hostname: &hostname,

FailoverTimeout: &failoverTimeout,

Checkpoint: &checkpoint,

}

log.Fatal(subscribe())

}

In the last line, we call the subscribe() function. Now it's time to implement it. This will be the biggest function. In the book example, we skipped error handling but remember that it's an important part of writing robust software in Go:

func subscribe() error {

subscribeCall := &Call{

Type: Call_SUBSCRIBE.Enum(),

Subscribe: &Call_Subscribe{FrameworkInfo: &frameworkInfo},

}

body, _ := marshaller.MarshalToString(subscribeCall)

log.Print(body)

res, _ := http.Post(schedulerApiUrl, "application/json", bytes.NewBuffer([]byte(body)))

defer res.Body.Close()



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.